let network = require('../../utils/network.js')
const jiayouMgr = require('../../store/jiayouStore.js')
var observer = require('../../libs/observer').observer;

Page(observer({
  props: {
    jiayouStore: jiayouMgr.store
  },
  data: {
    GasCards: []
  },

  // 点击
  cardClick: function(e) {
    let item = e.currentTarget.dataset.item;
    jiayouMgr.store.activeGasCard = item;
    wx.navigateBack();
  },

  // 添加
  addCard: function() {
    wx.navigateTo({
      url: "/pages/jiayou/gasCardAdd"
    });
  },

  // 删除
  delItem: function(e) {
    var itemid = e.currentTarget.dataset.item.id;
    jiayouMgr.remove(itemid, () => {
      this.list();
    });
  },

  onShow: function() {
    this.list();
  },

  list: function() {
    const reg = /.{4}/g;
    jiayouMgr.list(list => {
      const array = list.map(c => {
        let cardNum = c.cardNum;
        if (cardNum) {
          cardNum = cardNum.match(reg).join(' ')
          return Object.assign({}, c, {
            cardNum: cardNum
          })
        }
        return c;
      })
      this.setData({
        GasCards: array,
      })
    });
  }
}))